<?php

declare (strict_types=1);
namespace App\Model\admin;

use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;

/**
 * @property int $id ID
 * @property int $pid 父id
 * @property string $title 名称
 * @property string $icon 图标
 * @property string $route 前端路由
 * @property string $component 前端组件名（页面）
 * @property string $controller 控制器
 * @property string $action 请求方法
 * @property string $target 链接打开方式
 * @property int $sort 排序
 * @property int $is_menu 是否菜单(0:否,1:是)
 * @property int $status 状态(0:禁用,1:启用)
 * @property string $remark 备注
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property \Carbon\Carbon $deleted_at 删除时间
 */
class SystemPermission extends Model{
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'system_permission';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'pid', 'title', 'icon', 'route', 'component', 'controller', 'action', 'target', 'sort', 'is_menu', 'status', 'remark', 'created_at', 'updated_at', 'deleted_at'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'pid' => 'integer', 'sort' => 'integer', 'is_menu' => 'integer', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'integer'];

    /**
     * 根据url获取权限
     * @param $controller
     * @param $action
     * @return array
     */
    public function getPermissionByUrl($controller,$action){
        $menus = $this->getAllPermissions();

        return $menus[$controller.'.'.$action] ?? ['id' => 0,'title'=>''];
    }

    /**
     * @Cacheable(prefix="get-all-key-menus", ttl=9000)
     * @return array
     */
    public function getAllPermissions(): array
    {
        $menus = $this->query()->where(['status'=> 1])->get(['id','title','controller','action'])->toArray();
        $resArr = array();
        foreach ($menus as $menu){
            $key = $menu['controller'] . '.' . $menu['action'];
            if (empty($key))
                $key = 'Empty.empty';
            unset($menu['controller'],$menu['action']);
            $resArr[$key] = $menu;
        }
        return $resArr;
    }

//* @Cacheable(prefix="get-menu-by-user-id",value="#{uid}", ttl=9000)

    /**
     * @param $roleIds
     * @return array
     */
    public function getPermissionsByRoleIds($roleIds,$uid): array
    {
        $m = $this->query()->where('is_menu',1)
            ->where('status',1);

        if ($uid!=1 || !in_array(1,$roleIds)){
            $ids = SystemRolePermission::getAuthByRoleId($roleIds);
            $m->whereIn('id',$ids);
        }
        $list = $m->orderByDesc('sort')
            ->get(['id','pid','title','icon','route','component','target']);

        return buildTree($list);
    }
}